<?php
namespace Api\Controller;
use Think\Controller;
class JiaoyuController extends CommonController {
    public function news(){
        $map = array();
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        $map['type'] = '';
        $list = M("JiaoyuNews")->where($map)->order("last_time desc")->select();
        // foreach ($list as $key => $value) {
        //     $list[$key]['pics'] = explode(',', $value['pics']);
        // }
        $this->ajaxReturn($list);
    }

    public function newsItem(){
        if(is_numeric($_GET['id'])){
            $map['id'] = $_GET['id'];
        }else{
            $map['type'] = $_GET['type'];
            $map['shop_id'] = $_GET['shop_id'];
        }
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("JiaoyuNews")->where($map)->find();
        $list['pics'] = explode(',', $list['pics']);
        $this->ajaxReturn($list);
    }
    public function newsAdd(){
        $eq['wid'] = session('wid');
        $eq['openid'] = I("openid");
        $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        $map['wid'] = session('wid');
        $type = I("type");
        $_POST['status'] = 1;
        $_POST['create_time'] = date("Y-m-d H:i:s");
        if($type){
            $map['type'] = $type;
            if(M("JiaoyuNews")->where($map)->find()){
                M("JiaoyuNews")->where($map)->save($_POST);
            }else{
                $_POST['shop_id'] = $map['shop_id'];
                $_POST['type'] = $type;
                M("JiaoyuNews")->add($_POST);
            }
        }else{
            $_POST['shop_id'] = $map['shop_id'];
            M("JiaoyuNews")->add($_POST);
        }
        $arr = array("err_code"=>0,"err_msg"=>"添加成功！");
        $this->ajaxReturn($arr);
    }
    public function newsForm(){
        $type = I("type");
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }
        $map['type'] = I("type");
        $info = M("JiaoyuNews")->where($map)->find();
        $pics = explode(',', $info['pics']);
        // dump($info);
        if($type == 'about'){
            $form[] = array('type'=>'textarea', 'title'=>'关于我们', 'name'=>'content', 'value'=>$info['content']);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'图片', 'name'=>'pics', 'num'=>2, 'pics'=>!empty($pics[0])?$pics:array());
        }elseif($type == 'huanjin'){
            $form[] = array('type'=>'pic', 'title'=>'环境图片', 'name'=>'pics', 'num'=>2, 'pics'=>!empty($pics[0])?$pics:array());
        }else{
            $form[] = array('type'=>'text', 'title'=>'新闻标题', 'name'=>'title', 'value'=>$info['title']);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'一句话简介', 'name'=>'remark', 'value'=>$info['remark']);
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'pic', 'title'=>'新闻图片', 'name'=>'pics', 'num'=>2, 'pics'=>!empty($pics[0])?$pics:array());
            $form[] = array('type'=>'top');
            $form[] = array('type'=>'textarea', 'title'=>'详细介绍', 'name'=>'content', 'value'=>$info['content']);
        }
        // $res['form'] = $form;
        $this->ajaxReturn($form);
    }

    public function teacher(){
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        $map['type'] = $_GET['type'];
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("JiaoyuMember")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function memberAdd(){
        $eq['openid'] = I('openid');
        $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        // $new['question'] = I("question");
        // $new['answer'] = I("answer");

        if(M("JiaoyuMember")->add($_POST)){
            $arr = array("err_code"=>0,"err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
            $this->ajaxReturn($arr);
        }
    }

    public function memberItem(){
        $map['id'] = $_GET['id'];
        // $map['name'] = array('like',"%{$_GET['keyword']}%");
        $list = M("JiaoyuMember")->where($map)->find();
        $list['pics'] = explode(',', $list['pics']);
        $this->ajaxReturn($list);
    }


    public function answerAdd(){
        $eq['openid'] = I('openid');
        $new['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        $new['question'] = I("question");
        $new['answer'] = I("answer");
        $new['status'] = 1;
        $new['create_time'] = date("Y-m-d H:i:s");

        if(M("JiaoyuAnswer")->add($new)){
            $arr = array("err_code"=>0,"err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
            $this->ajaxReturn($arr);
        }
    }
    public function answer(){
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }
        $list = M("JiaoyuAnswer")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function baomingAdd(){
        if(empty($_POST['username'])){
            $arr = array("err_code"=>400,"err_msg"=>"请填写联系人");
            $this->ajaxReturn($arr);
        }
        if(empty($_POST['tel'])){
            $arr = array("err_code"=>400,"err_msg"=>"请填写联系电话");
            $this->ajaxReturn($arr);
        }
        $new['wid'] = session('wid');
        $new['shop_id'] = I("shop_id");
        $new['username'] = I("username");
        $new['tel'] = I("tel");
        $new['content'] = I("content");
        $new['opneid'] = I("openid");
        if(M("JiaoyuBaoming")->add($new)){
            $arr = array("err_code"=>0,"err_msg"=>"报名成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！");
            $this->ajaxReturn($arr);
        }
    }
    public function baomingList(){
        $eq['openid'] = $_GET['openid'];
        $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");

        $list = M("JiaoyuBaoming")->where($map)->select();
        $this->ajaxReturn($list);
    }

    public function food(){
        $map['wid'] = session('wid');
        if($_GET['openid']){
            $eq['openid'] = $_GET['openid'];
            $map['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        }else{
            $map['shop_id'] = $_GET['shop_id'];
        }

        $types = array("早餐","中餐","晚餐");
        $list = M("JiaoyuFood")->where($map)->order("week asc,type asc")->select();
        foreach ($list as $key => $value) {
            $list[$key]['type'] = $types[$value['type']];
        }

        $this->ajaxReturn($list);
    }
    public function foodAdd(){
        foreach ($_POST as $key => $value) {
            if(!is_numeric($value) && empty($value)){
                $arr = array("err_code"=>400,"err_msg"=>"请填写全部信息！");
                $this->ajaxReturn($arr);
            }
        }
        $eq['openid'] = I('openid');
        $_POST['wid'] = session('wid');
        $_POST['shop_id'] = M("Shop")->where($eq)->getField("shop_id");
        if(M("JiaoyuFood")->add($_POST)){
            $arr = array("err_code"=>0,"err_msg"=>"添加成功！");
            $this->ajaxReturn($arr);
        }else{
            $arr = array("err_code"=>400,"err_msg"=>"网络忙！".M("JiaoyuFood")->getLastSql());
            $this->ajaxReturn($arr);
        }

    }
}